<template>
  <div>
    <van-popup position="right" :value="isShow" @open="closed">
      <van-nav-bar
        :title="total + '条回复'"
        left-arrow
        @click-left="leftClick"
      ></van-nav-bar>
      <div class="reply-wrapper list">
        <!-- 列表 -->
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="getData"
        >
          <div
            class="item van-hairline--bottom"
            v-for="i in list"
            :key="i.com_id"
          >
            <van-image
              round
              width="10vw"
              height="10vw"
              :src="localItem.aut_photo"
            />
            <div class="info">
              <p>
                <span class="name">{{ localItem.aut_name }}</span>
                <span class="zan"
                  >{{ localItem.like_count }} <geek-icon name="like2"
                /></span>
              </p>
              <p class="cont">{{ localItem.content }}</p>
              <p>
                <span class="time" style="margin-left:0">{{
                  localItem.pubdate | filterTimeGlobal
                }}</span>
              </p>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 底 -->

      <div class="footer van-hairline--top" style="position:static">
        <div class="input big" @click="inputClick">
          <i class="van-icon van-icon-edit"></i>
        </div>
        <div class="btn">
          <geek-icon name="collect"></geek-icon>
          <p>收藏</p>
        </div>
        <div class="btn">
          <geek-icon name="share"></geek-icon>
          <p>分享</p>
        </div>
      </div>
    </van-popup>
    <van-popup position="bottom" v-model="showSend" @close="CloseEvent">
      <van-nav-bar
        left-arrow
        :title="localItem === '' ? '发表评论' : '回复评论'"
        @click-left="showSend = false"
      >
        <template #right>
          <div style="color:#f40" @click="send">
            发表
          </div>
        </template>
      </van-nav-bar>
      <van-form ref="form" v-if="bol2">
        <van-field
          :rules="rules"
          v-model="text"
          rows="3"
          autosize
          type="textarea"
          maxlength="100"
          placeholder="请输入评论"
          show-word-limit
        >
        </van-field>
      </van-form>
    </van-popup>
  </div>
</template>

<script>
import { commentsGet, commentsPost } from '@/api/article.js'
export default {
  name: 'ReComment',
  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    localItem: {
      type: [String, Object],
      default: ''
    },
    commentList: {
      type: Array,
      default: () => []
    },
    info: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      bol2: true,
      text: '',
      showSend: false,
      total: 0,
      bol: true,
      list: [],
      loading: false,
      finished: false,
      params: {
        type: 'c',
        source: '',
        offset: null,
        limit: 10
      },
      rules: [
        {
          required: true,
          message: '必填',
          trigger: 'onChang'
        }
      ]
    }
  },
  model: {
    prop: 'isShow',
    event: 'closeShow'
  },
  methods: {
    leftClick () {
      this.$emit('closeShow', false)
    },
    async getData () {
      const res = await commentsGet({
        ...this.params,
        source: this.localItem.com_id
      })
      this.loading = false
      this.list.push(...res.data.results)
      if (res.data.end_id === res.data.last_id) {
        this.finished = true
      } else {
        this.params.offset = res.data.last_id
      }
      console.log('回复评论:', res)
      this.total = res.data.total_count
    },
    closed () {
      this.loading = false
      this.finished = false
      this.params.offset = null
      this.list = []
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    },
    inputClick () {
      this.showSend = true
    },
    async send () {
      await this.$refs.form.validate()
      if (this.localItem !== '') {
        const res = await commentsPost({
          target: this.localItem.com_id,
          content: this.text,
          art_id: this.$route.params.id
        })
        this.list.unshift(res.data.new_obj)
        console.log('回复的数据:', res)
        this.showSend = false
        this.text = ''
        this.total++
      } else {
        const res = await commentsPost({
          target: this.$route.params.id,
          content: this.text
        })
        this.commentList.unshift(res.data.new_obj)
        this.info.comm_count++
        console.log('发表返回的值:', res)
      }
      this.showSend = false
    },
    CloseEvent () {
      this.text = ''
      this.bol2 = false
      this.$nextTick(() => {
        this.bol2 = true
      })
    }
  },
  created () {
    // this.getData()
  }
}
</script>

<style lang="less" scoped>
.van-popup {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.reply-wrapper {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}
.article-comment {
  .title {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    background: #fff;
    span {
      font-size: 16px;
      &:last-child {
        color: #ccc;
        font-size: 14px;
      }
    }
  }
  .list {
    padding: 0 16px 30px;
    .item {
      display: flex;
      padding: 10px 0;
      .info {
        padding-left: 10px;
        flex: 1;
        p {
          margin: 0;
          .name {
            font-size: 16px;
          }
          .zan {
            font-size: 14px;
            float: right;
            color: #999;
            .geek-icon {
              font-size: 12px;
              position: relative;
              top: -1px;
            }
          }
          &.cont {
            font-size: 14px;
            color: #666;
            padding: 10px 0;
            word-break: break-all;
            padding-right: 40px;
          }
          .reply {
            min-width: 60px;
            height: 24px;
            text-align: center;
            line-height: 28px;
            font-size: 12px;
            background: @geek-gray-color;
            display: inline-block;
            border-radius: 14px;
            color: #666;
            .van-icon {
              position: relative;
              top: 1px;
            }
          }
          .time {
            font-size: 12px;
            color: #999;
            margin-left: 10px;
          }
        }
      }
    }
  }
}
.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 50px;
  background: #fff;
  display: flex;
  width: 100%;
  align-items: center;
  .input {
    margin-left: 10px;
    width: 200px;
    height: 34px;
    background: @geek-gray-color;
    border-radius: 17px;
    line-height: 36px;
    padding-left: 10px;
    .van-icon {
      color: #999;
    }
    &.big {
      width: 260px;
    }
  }
  .btn {
    flex: 1;
    text-align: center;
    position: relative;
    p {
      margin: 0;
      font-size: 10px;
    }
    .geek-icon {
      font-size: 18px;
    }
    i {
      height: 16px;
      min-width: 16px;
      padding: 0 3px;
      background: @geek-color;
      color: #fff;
      font-size: 10px;
      position: absolute;
      right: 0;
      top: -4px;
      line-height: 16px;
      border-radius: 8px;
      font-style: normal;
    }
  }
}
::v-deep .van-nav-bar__text {
  color: @geek-color;
}
::v-deep .van-field__control {
  background: @geek-gray-color;
  padding: 5px 10px;
  margin-bottom: 5px;
  border-radius: 4px;
}
</style>
